package com.atguiju.preparedstatement.crud;

import com.atguiju.connection.ConnectionTest;
import com.atguiju.util.JDBCUtils;
import org.junit.Test;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class PreparedStatementUpdateTest {

    @Test
    public void TestInsert() {
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = JDBCUtils.getConnection();

            //4.预编译sql语句，返回PreparedStatement的实例
            String sql = "insert into customers(name, email, birth) value (?, ?, ?)";
            ps = conn.prepareStatement(sql);
            //5.填充占位符,凡是和数据库交互的时候，索引都是从1开始
            ps.setString(1, "谢一双");
            ps.setString(2, "ddfs@gmail.com");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = simpleDateFormat.parse("1000-01-01");
            ps.setDate(3, new java.sql.Date(parse.getTime()));
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            JDBCUtils.close(conn, ps);
        }
    }
}
